﻿using System;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using Loki.Utils;

namespace Loki.UI.Win
{
    public class HandleToIndexConverter : LokiObject, IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            GridView L_Parameter = parameter as GridView;
            if (L_Parameter != null)
            {
                int L_Index = (int)value;
                if (L_Index == -1)
                    return GridControl.InvalidRowHandle;
                else
                    return L_Parameter.GetRowHandle((int)value);
            }
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            GridView L_Parameter = parameter as GridView;
            if (L_Parameter != null)
            {
                int L_Handle = (int)value;
                if (L_Parameter.IsValidRowHandle(L_Handle))
                    return L_Parameter.GetDataSourceRowIndex(L_Handle);
                else
                    return -1;
            }
            return value;
        }
    }
}
